﻿namespace AnyOffice.Components
{
    using log4net;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.Caching;
    using System.Xml;

    public class ConfigManager
    {
        private int _administratorPrivId = 1;
        private int _ajaxInterval = 0xea60;
        private int _attachMaxSize = 0x6400000;
        private int _bbsNum = 5;
        private int _emailNum = 5;
        private int _imageUploadMaxSize = 0x100000;
        private bool _loginKey = false;
        private string _mobileOutPriv = string.Empty;
        private string _mobileRemindPriv = string.Empty;
        private string _mobileTypePriv = string.Empty;
        private string _myTableLeft = string.Empty;
        private string _myTableRight = string.Empty;
        private int _newsNum = 5;
        private int _notifyNum = 5;
        private int _pageSize = 10;
        private bool _rtxEnabled = false;
        private bool _securityRember = true;
        private bool _securityVerifyCode = false;
        private bool _skypeButtonEnabled = false;
        private string _sqlForResCallback = string.Empty;
        private string _sqlForUserDelete = string.Empty;
        private int _voteNum = 5;
        private static readonly ILog log = LogManager.GetLogger(typeof(ConfigManager));
        public static string Version = "4.1.1106";
        public static string VersionName = "";

        public ConfigManager()
        {
            if (HttpContext.Current.Cache["AnyOffice_Config"] == null) this.Insert();
        }

        private void Insert()
        {
            try
            {
                Hashtable section = (Hashtable) ConfigurationManager.GetSection("anyoffice");
                string filename = HttpContext.Current.Server.MapPath("/Web.config");
                XmlDocument document = new XmlDocument();
                document.Load(filename);
                XmlNode node = document.SelectSingleNode("configuration/rtx");
                if (node != null) section["anyoffice/global/rtx_enabled"] = Convert.ToBoolean(node.Attributes["enabled"].Value);
                string str2 = "SELECT [TypePriv],[RemindPriv],[OutPriv] FROM [dbo].[Mobile_Priv]";
                SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, str2);
                if (reader.Read())
                {
                    section["anyoffice/mobile/typepriv"] = !reader.IsDBNull(0) ? reader.GetString(0) : string.Empty;
                    section["anyoffice/mobile/remindpriv"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                    section["anyoffice/mobile/outpriv"] = !reader.IsDBNull(2) ? reader.GetString(2) : string.Empty;
                }
                reader.Close();
                str2 = "SELECT [ParaName],[ParaValue] FROM [dbo].[Sys_Para] WHERE [ParaName] IN ('MYTABLE_LEFT','MYTABLE_RIGHT','SECURITY_VERIFYCODE','SECURITY_REMBER','LOGIN_KEY')";
                reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, str2);
                while (reader.Read())
                {
                    string str3 = reader.GetString(0).ToUpper();
                    if (str3 != null)
                    {
                        if (str3 != "MYTABLE_LEFT")
                        {
                            if (str3 == "MYTABLE_RIGHT")
                            {
                                section["anyoffice/mytable/right"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                                continue;
                            }
                            if (str3 == "SECURITY_VERIFYCODE")
                            {
                                section["anyoffice/global/security_verifycode"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                                continue;
                            }
                            if (str3 == "SECURITY_REMBER")
                            {
                                section["anyoffice/global/security_rember"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                                continue;
                            }
                            if (str3 == "LOGIN_KEY")
                            {
                                section["anyoffice/global/login_key"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                                continue;
                            }
                        }
                        else
                            section["anyoffice/mytable/left"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                    }
                }
                reader.Close();
                section = Hashtable.Synchronized(section);
                HttpContext.Current.Cache.Insert("AnyOffice_Config", section, new CacheDependency(HttpContext.Current.Server.MapPath("/web.config")), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled) { log.Error("", exception); }
            }
        }

        public static void Update()
        {
            try
            {
                Hashtable section = (Hashtable) ConfigurationManager.GetSection("anyoffice");
                string filename = HttpContext.Current.Server.MapPath("/Web.config");
                XmlDocument document = new XmlDocument();
                document.Load(filename);
                XmlNode node = document.SelectSingleNode("configuration/rtx");
                if (node != null) section["anyoffice/global/rtx_enabled"] = Convert.ToBoolean(node.Attributes["enabled"].Value);
                string str2 = "SELECT [TypePriv],[RemindPriv],[OutPriv] FROM [dbo].[Mobile_Priv]";
                SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, str2);
                if (reader.Read())
                {
                    section["anyoffice/mobile/typepriv"] = !reader.IsDBNull(0) ? reader.GetString(0) : string.Empty;
                    section["anyoffice/mobile/remindpriv"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                    section["anyoffice/mobile/outpriv"] = !reader.IsDBNull(2) ? reader.GetString(2) : string.Empty;
                }
                reader.Close();
                str2 = "SELECT [ParaName],[ParaValue] FROM [dbo].[Sys_Para] WHERE [ParaName] IN ('MYTABLE_LEFT','MYTABLE_RIGHT','SECURITY_VERIFYCODE','SECURITY_REMBER','LOGIN_KEY')";
                reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, str2);
                while (reader.Read())
                {
                    string str3 = reader.GetString(0).ToUpper();
                    if (str3 != null)
                    {
                        if (str3 != "MYTABLE_LEFT")
                        {
                            if (str3 == "MYTABLE_RIGHT")
                            {
                                section["anyoffice/mytable/right"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                                continue;
                            }
                            if (str3 == "SECURITY_VERIFYCODE")
                            {
                                section["anyoffice/global/security_verifycode"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                                continue;
                            }
                            if (str3 == "SECURITY_REMBER")
                            {
                                section["anyoffice/global/security_rember"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                                continue;
                            }
                            if (str3 == "LOGIN_KEY")
                            {
                                section["anyoffice/global/login_key"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                                continue;
                            }
                        }
                        else
                            section["anyoffice/mytable/left"] = !reader.IsDBNull(1) ? reader.GetString(1) : string.Empty;
                    }
                }
                reader.Close();
                section = Hashtable.Synchronized(section);
                HttpContext.Current.Cache.Insert("AnyOffice_Config", section, new CacheDependency(HttpContext.Current.Server.MapPath("/web.config")), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled) { log.Error("", exception); }
            }
        }

        public int AdministratorPrivId
        {
            get
            {
                try
                {
                    return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/administrator_privid"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._administratorPrivId;
                }
            }
        }

        public int AjaxInterval
        {
            get
            {
                try
                {
                    return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/ajax_interval"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._ajaxInterval;
                }
            }
        }

        public static string ApplicationName
        {
            get
            {
                return "上海市静态交通办公室网络信息平台";
            }
        }

        public static string ApplicationShortName
        {
            get
            {
                return "上海市静态交通办公室网络信息平台";
            }
        }

        public int AttachMaxSize
        {
            get
            {
                try
                {
                    return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/attach_maxsize"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._attachMaxSize;
                }
            }
        }

        public int BbsNum
        {
            get
            {
                try
                {
                    return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/bbs_num"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._bbsNum;
                }
            }
        }

        public static string ConnectionString
        {
            get
            {
                return ConfigurationManager.AppSettings["ConnectionString"];
            }
        }

        public int EmailNum
        {
            get
            {
                try
                {
                    return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/email_num"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._emailNum;
                }
            }
        }

        public int ImageUploadMaxSize
        {
            get
            {
                try
                {
                    return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/imageupload_maxsize"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._imageUploadMaxSize;
                }
            }
        }

        public bool LoginKey
        {
            get
            {
                try
                {
                    return Convert.ToBoolean(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/login_key"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._loginKey;
                }
            }
        }

        public string MobileOutPriv
        {
            get
            {
                try
                {
                    return (string) ((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/mobile/outpriv"];
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._mobileOutPriv;
                }
            }
        }

        public string MobileRemindPriv
        {
            get
            {
                try
                {
                    return (string) ((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/mobile/remindpriv"];
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._mobileRemindPriv;
                }
            }
        }

        public string MobileTypePriv
        {
            get
            {
                try
                {
                    return (string) ((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/mobile/typepriv"];
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._mobileTypePriv;
                }
            }
        }

        public string MyTableLeft
        {
            get
            {
                try
                {
                    return Convert.ToString(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/mytable/left"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._myTableLeft;
                }
            }
        }

        public string MyTableRight
        {
            get
            {
                try
                {
                    return Convert.ToString(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/mytable/right"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._myTableRight;
                }
            }
        }

        public int NewsNum
        {
            get
            {
                try
                {
                    return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/news_num"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._newsNum;
                }
            }
        }

        public int NotifyNum
        {
            get
            {
                try
                {
                    return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/notify_num"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._notifyNum;
                }
            }
        }

        public int PageSize
        {
            get
            {
                try
                {
                    return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/page_size"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._pageSize;
                }
            }
        }

        public bool RtxEnabled
        {
            get
            {
                try
                {
                    return Convert.ToBoolean(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/rtx_enabled"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._rtxEnabled;
                }
            }
        }

        public bool SecurityRember
        {
            get
            {
                try
                {
                    return Convert.ToBoolean(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/security_rember"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._securityRember;
                }
            }
        }

        public bool SecurityVerifyCode
        {
            get
            {
                try
                {
                    return Convert.ToBoolean(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/security_verifycode"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._securityVerifyCode;
                }
            }
        }

        public bool SkypeButtonEnabled
        {
            get
            {
                try
                {
                    return Convert.ToBoolean(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/skype_button"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._skypeButtonEnabled;
                }
            }
        }

        public string SqlForResCallback
        {
            get
            {
                try
                {
                    return (string) ((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/sqlfor_rescallback"];
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._sqlForResCallback;
                }
            }
        }

        public string SqlForUserDelete
        {
            get
            {
                try
                {
                    return (string) ((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/sqlfor_userdelete"];
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._sqlForUserDelete;
                }
            }
        }

        public int VoteNum
        {
            get
            {
                try
                {
                    return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_Config"])["anyoffice/global/vote_num"]);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) { log.Error("", exception); }
                    return this._voteNum;
                }
            }
        }
    }
}

